package com.xudow.app.dynamicstate_old.module.follow;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.jude.beam.bijection.Presenter;
import com.jude.library.imageprovider.ImageProvider;
import com.jude.library.imageprovider.OnImageSelectListener;
import com.jude.utils.JUtils;
import com.umeng.share.ShareManager;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.widget.exview.PieceViewGroup;

import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by ske on 2016/6/16.
 */
public class NewDynamicPresenter extends Presenter<NewDynamicActivity> implements PieceViewGroup.OnViewDeleteListener {
    private ImageProvider provider;
    private ArrayList<Uri> uriArrayList = new ArrayList<Uri>();
    private boolean isShareQQ = false;
    private boolean isShareWeChat = false;
    private boolean isShareWeibo = false;

    public void setShareWeChat(boolean shareWeChat) {
        isShareWeChat = shareWeChat;
    }

    public void setShareWeibo(boolean shareWeibo) {
        isShareWeibo = shareWeibo;
    }

    public void setShareQQ(boolean shareQQ) {
        isShareQQ = shareQQ;
    }

    public boolean isShareWeChat() {
        return isShareWeChat;
    }

    public boolean isShareQQ() {
        return isShareQQ;
    }

    public boolean isShareWeibo() {
        return isShareWeibo;
    }

    OnImageSelectListener listener = new OnImageSelectListener() {

        @Override
        public void onImageSelect() {
            getView().getExpansion().showProgressDialog("加载中");
        }

        @Override
        public void onImageLoaded(Uri uri) {
            getView().getExpansion().dismissProgressDialog();
            getView().addImage(ImageProvider.readImageWithSize(uri, 300, 300));
            uriArrayList.add(uri);
        }

        @Override
        public void onError() {

        }
    };

    public ArrayList<Uri> getUriArrayList() {
        return uriArrayList;
    }

    @Override
    protected void onCreate(NewDynamicActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        provider = new ImageProvider(getView());
    }

    public void editFace(int type) {
        if (uriArrayList.size() >= 9) {
            getView().showShortToast("最多上传9张图片");
            return;
        }
        switch (type) {
            case 0:
                provider.getImageFromCamera(listener);
                break;
            case 1:
                provider.getImageFromAlbum(listener, 9 - uriArrayList.size());
                break;
        }
    }

    @Override
    protected void onResult(int requestCode, int resultCode, Intent data) {
        super.onResult(requestCode, resultCode, data);
        provider.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onViewDelete(int i) {
        uriArrayList.remove(i);
    }

    public void submit(final Dynamic dynamic, String attentions, String subjects) {
        int size = uriArrayList.size();
        if (size > 0) {
            File[] files = new File[size];
            for (int i = 0; i < size; i++) {
                files[i] = new File(uriArrayList.get(i).getPath());
            }
            ImageModel.getInstance().putImageSync(files)
                    .doAfterTerminate(() -> getView().hideLoadingDialog())
                    .toList()
                    .flatMap(strings -> {
                        StringBuilder sb = new StringBuilder();
                        for (String path : strings) {
                            sb.append(path).append(";");
                        }
                        if (sb.length() != 1) {
                            sb.deleteCharAt(sb.length() - 1);
                            dynamic.setImgpath(sb.toString());
                        }
                        return DynamicModel.getInstance().newDynamic(dynamic, attentions, subjects);
                    })
                    .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
//                    .flatMap(m -> share(dynamic))
                    .subscribe(m -> {
                        share(dynamic);
                        getView().setResult(Activity.RESULT_OK);
                        getView().showShortToast("发表成功");
                        getView().finish();
                    });
        } else {
            DynamicModel.getInstance().newDynamic(dynamic, attentions, subjects)
                    .doAfterTerminate(() -> getView().hideLoadingDialog())
                    .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
//                    .flatMap(m -> share(dynamic))
                    .subscribe(m -> {
                        share(dynamic);
                        getView().setResult(Activity.RESULT_OK);
                        getView().showShortToast("发表成功");
                        getView().finish();
                    });
        }
    }

    private void share(Dynamic dynamic) {
        boolean emptyImage = TextUtils.isEmpty(dynamic.getImgpath());
        if (isShareQQ())
            ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.QZONE, getView(), dynamic.getContent(), "http://app.xudow.com/p/?plg_nld=1&plg_uin=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
                    new UMShareListener() {
                        @Override
                        public void onResult(SHARE_MEDIA platform) {
                            JUtils.Toast("分享成功");
                        }

                        @Override
                        public void onError(SHARE_MEDIA platform, Throwable t) {
                            JUtils.Toast("分享失败");
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA platform) {
                            JUtils.Toast("取消分享");
                        }
                    });
        if (isShareWeChat())
            ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.WEIXIN_CIRCLE, getView(), dynamic.getContent(), "http://app.xudow.com/p/?plg_nld=1&plg_uin=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
                    new UMShareListener() {
                        @Override
                        public void onResult(SHARE_MEDIA platform) {
                            JUtils.Toast("分享成功");
                        }

                        @Override
                        public void onError(SHARE_MEDIA platform, Throwable t) {
                            JUtils.Toast("分享失败");
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA platform) {
                            JUtils.Toast("取消分享");
                        }
                    });
        if (isShareWeibo())
            ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.SINA, getView(), dynamic.getContent(), "http://app.xudow.com/p/?plg_nld=1&plg_uin=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
                    new UMShareListener() {
                        @Override
                        public void onResult(SHARE_MEDIA platform) {
                            JUtils.Toast("分享成功");
                        }

                        @Override
                        public void onError(SHARE_MEDIA platform, Throwable t) {
                            JUtils.Toast("分享失败");
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA platform) {
                            JUtils.Toast("取消分享");
                        }
                    });
    }

//    private Observable<Object> share(Dynamic dynamic) {
//        boolean emptyImage = TextUtils.isEmpty(dynamic.getImgpath());
//        return Observable.create(new Observable.OnSubscribe<Object>() {
//            @Override
//            public void call(Subscriber<? super Object> subscriber) {
//                if (isShareQQ())
//                    ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.QZONE, getView(), dynamic.getContent(), "http://www.xudow.com", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
//                            new UMShareListener() {
//                                @Override
//                                public void onResult(SHARE_MEDIA platform) {
//                                    JUtils.Toast("分享成功");
//                                    subscriber.onNext(null);
//                                }
//
//                                @Override
//                                public void onError(SHARE_MEDIA platform, Throwable t) {
//                                    JUtils.Toast("分享失败");
//                                    subscriber.onNext(null);
//                                }
//
//                                @Override
//                                public void onCancel(SHARE_MEDIA platform) {
//                                    JUtils.Toast("取消分享");
//                                    subscriber.onNext(null);
//                                }
//                            });
//                else
//                    subscriber.onNext(null);
//            }
//        })
//                //分享微信
//                .flatMap(o -> Observable.create(new Observable.OnSubscribe<Object>() {
//                    @Override
//                    public void call(Subscriber<? super Object> subscriber) {
//                        if (isShareWeChat())
//                            ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.WEIXIN_CIRCLE, getView(), dynamic.getContent(), "http://www.xudow.com", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
//                                    new UMShareListener() {
//                                        @Override
//                                        public void onResult(SHARE_MEDIA platform) {
//                                            JUtils.Toast("分享成功");
//                                            subscriber.onNext(null);
//                                        }
//
//                                        @Override
//                                        public void onError(SHARE_MEDIA platform, Throwable t) {
//                                            JUtils.Toast("分享失败");
//                                            subscriber.onNext(null);
//                                        }
//
//                                        @Override
//                                        public void onCancel(SHARE_MEDIA platform) {
//                                            JUtils.Toast("取消分享");
//                                            subscriber.onNext(null);
//                                        }
//                                    });
//                        else
//                            subscriber.onNext(null);
//                    }
//                }))
//                //分享微博
//                .flatMap(o -> Observable.create(new Observable.OnSubscribe<Object>() {
//                    @Override
//                    public void call(Subscriber<? super Object> subscriber) {
//                        if (isShareWeibo())
//                            ShareManager.getInstance(getView()).shareContent(SHARE_MEDIA.SINA, getView(), dynamic.getContent(), "http://www.xudow.com", "学兜分享", emptyImage ? "" : dynamic.getImgpath().split(";")[0],
//                                    new UMShareListener() {
//                                        @Override
//                                        public void onResult(SHARE_MEDIA platform) {
//                                            JUtils.Toast("分享成功");
//                                            subscriber.onNext(null);
//                                        }
//
//                                        @Override
//                                        public void onError(SHARE_MEDIA platform, Throwable t) {
//                                            JUtils.Toast("分享失败");
//                                            subscriber.onNext(null);
//                                        }
//
//                                        @Override
//                                        public void onCancel(SHARE_MEDIA platform) {
//                                            JUtils.Toast("取消分享");
//                                            subscriber.onNext(null);
//                                        }
//                                    });
//                        else
//                            subscriber.onNext(null);
//                    }
//                }));
//    }
}
